﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC3MusicStore.Models;
using MVC3MusicStore.Resources;
using System.Linq.Dynamic;
using PagedList;
using MVC3MusicStore.Helpers;

namespace MVC3MusicStore.Controllers
{
    public class StoreManagerController : Controller
    {
        //
        // GET: /StoreManager/

        MusicStoreEntities db = new MusicStoreEntities();

        /// <summary>
        /// 主页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string sortOrder, string filter,string Keyword, int page = 1)
        {
            //return View();
            //var albums = db.Albums.Include("Genre").Include("Artist"); 
            //return View(albums.ToList());

            #region ViewBag资源
            ViewBag.CreateLink = Resource.CreateLink;
            ViewBag.EditLink = Resource.EditLink;
            ViewBag.DetailsLink = Resource.DetailsLink;
            ViewBag.DeleteLink = Resource.DeleteLink;
            ViewBag.GenreDisplay = Resource.GenreDisplay;
            ViewBag.ArtistDisplay = Resource.ArtistDisplay;
            ViewBag.TitleDisplay = Resource.TitleDisplay;
            ViewBag.PriceDisplay = Resource.PriceDisplay;
            #endregion

            #region ViewBag排序PARAMS
            ViewBag.GenreSortParam = (sortOrder == "Genre.Name") ? "Genre.Name desc" : "Genre.Name";
            ViewBag.ArtistSortParam = (sortOrder == "Artist.Name") ? "Artist.Name desc" : "Artist.Name";
            ViewBag.TitleSortParam = (sortOrder == "Title") ? "Title desc" : "Title";
            ViewBag.PriceSortParam = (sortOrder == "Price") ? "Price desc" : "Price";

            //默认的排序顺序
            if (String.IsNullOrEmpty(sortOrder))
            {
                sortOrder = "Title desc";
            }

            ViewBag.CurrentSortOrder = sortOrder;
            #endregion

            #region 过滤器
            var albums = from a in db.Albums select a;
            switch (filter)
            {
                case "Less":
                    albums = albums.Where(a => a.Price
                    <= 10
                    );
                    break;
                case "Greater":
                    albums = albums.Where(a => a.Price >
                    10);
                    break;
                default:
                    // No filter needed
                    break;
            }
            ViewBag.CurrentFilter =
            String.IsNullOrEmpty(filter) ? "" : filter;
            #endregion

            #region 关键字搜索
            if (!String.IsNullOrEmpty(Keyword))
            {
                albums = albums.Where(a =>
                a.Title.ToUpper().Contains(Keyword.ToUpper())
                || a.Title.ToUpper().Contains(
                Keyword.ToUpper()));
            }
            ViewBag.CurrentKeyword =
            String.IsNullOrEmpty(Keyword) ? "" : Keyword;
            #endregion

            //var albums = db.Albums.OrderBy(sortOrder);
            albums = albums.OrderBy(sortOrder);
            int maxRecords = 10;
            int currentPage = page;
            return View(albums.ToPagedList(currentPage, maxRecords));
        }

        //
        // GET: /StoreManager/Details/5

        /// <summary>
        /// 详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            //return View();
            Album album = db.Albums.Find(id);
            return View(album);
        }

        //
        // GET: /StoreManager/Create

        /// <summary>
        /// 创建
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name"); 
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name");
            return View();
        } 

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Album album, HttpPostedFileBase file)
        {
            //try
            //{
            //    // TODO: Add insert logic here

            //    return RedirectToAction("Index");
            //}
            //catch
            //{
            //    return View();
            //}

            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);
        }
        
        //
        // GET: /StoreManager/Edit/5
 
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            //return View();
            Album album = db.Albums.Find(id);
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Album album)
        {
            //try
            //{
            //    // TODO: Add update logic here

            //    Album album = db.Albums.Find(id);
            //    if (this.TryUpdateModel<MVC3MusicStore.Models.Album>(album))
            //    {
            //        return RedirectToAction("Index");
            //    }
            //    return View();
            //}
            //catch
            //{
            //    return View();
            //}

            if (ModelState.IsValid)
            {
                db.Entry(album).State = System.Data.EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);
        }

        //
        // GET: /StoreManager/Delete/5
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            //return View();
            Album album = db.Albums.Find(id);
            return View(album);
        }

        //
        // POST: /StoreManager/Delete/5

        //[HttpPost]
        //public ActionResult Delete(int id, FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add delete logic here
        //        MVC3MusicStore.Models.Album album = db.Albums.Find(id); 
        //        db.Albums.Remove(album); 
        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Album album = db.Albums.Find(id);
            db.Albums.Remove(album);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
